package com.cws.controller;

import java.util.HashMap;
import java.util.List;







import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONStringer;

import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.support.json.JSONParser;
import com.alibaba.druid.support.json.JSONUtils;
import com.cws.bridge.CommonConstant;
import com.cws.business.IActitviyBusiness;
import com.cws.model.vo.ProcessDefinitionVO;
import com.cws.model.vo.TaskVO;
import com.cws.web.GridResponseData;
import com.cws.web.ReponseData;
import com.cws.web.UserSession;



@Controller
public class ActivityController extends BaseController {
	
	@Autowired
	private IActitviyBusiness actitviyBusiness; 
	
	
	
	@RequestMapping("/listDefinitions")
	public ModelAndView listProcessDefinition(){
		ModelAndView modelAndView = new ModelAndView("jsp/activity/process");
		List<ProcessDefinitionVO> definitions = actitviyBusiness.getAllProcessDefinition();
		
		modelAndView.addObject("definitions", JSONArray.fromCollection(definitions).toString());
		return modelAndView;
	}
	
	@ResponseBody  
	@RequestMapping("/startProcess")
	public Map<String, Object> startProcess(@RequestParam String key){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(key)){
			Map<String, Object> variables = new HashMap<String, Object>(); 
			UserSession userSession =  new UserSession("true");
			variables.put("userSession", userSession);
			ProcessInstance instance = actitviyBusiness.startProcess(key,variables);
			modelMap.put("status",true);//
		}
		return modelMap;
	}
	
	@ResponseBody  
	 @RequestMapping("/getDefinitions")
	public GridResponseData<ProcessDefinitionVO> getDefinitions(HttpServletRequest request){
		List<ProcessDefinitionVO> definitions = actitviyBusiness.getAllProcessDefinition();
		return transformDataPage(request,definitions);
	}
	
	@ResponseBody  
	@RequestMapping(value="/getTaskList")
	public GridResponseData<TaskVO> getTaskList(HttpServletRequest request){
		List<TaskVO> taskList = actitviyBusiness.getAllTaskList();
		return transformDataPage(request,taskList);
	}
	
	@ResponseBody  
	@RequestMapping("ajax/claimTask")
	public ReponseData claimTask(HttpServletRequest request,@RequestParam String taskId ){
		 UserSession userSession = (UserSession) request.getSession().getAttribute(CommonConstant.USER_CONTEXT);
		 String userId = userSession.getUserId();
		boolean f = false;
		if(StringUtils.isNotBlank(taskId)){
			f = actitviyBusiness.claimTask(taskId, userId);
		}
		return f?ReponseData.SUCCESS:ReponseData.FAILURE;
	}
	
	
	
	
	
}
